package com.yu.shortlink.project.toolkit;

/**
 * @Author yushiwang
 * @Date 2024/6/20 21:16
 * @PackageName:com.yu.shortlink.project.toolkit
 * @ClassName: LinkUtil
 * @Version 1.0
 */

import cn.hutool.core.date.DateUtil;
import jakarta.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.Optional;

import static com.yu.shortlink.project.common.constant.ShortLinkConstant.DEFAULT_CACHE_VALID_TIME;

/**
 * 短链接工具类
 */
public class LinkUtil {

    /**
     * 获取短链接缓存有效期时间
     * @param validDate 有效期
     * @return 时间s
     */
    public static long getLinkCacheValidDate(Date validDate){
        return Optional.ofNullable(validDate)
                .map(each-> DateUtil.betweenMs(new Date(),validDate))
                .orElse(DEFAULT_CACHE_VALID_TIME);

    }

    /**
     * 获取用户访问操作系统
     * @return 操作系统
     */
    public static String getOs(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.toLowerCase().contains("windows")) {
            return "Windows";
        } else if (userAgent.toLowerCase().contains("mac")) {
            return "Mac OS";
        } else if (userAgent.toLowerCase().contains("linux")) {
            return "Linux";
        } else if (userAgent.toLowerCase().contains("unix")) {
            return "Unix";
        } else if (userAgent.toLowerCase().contains("android")) {
            return "Android";
        } else if (userAgent.toLowerCase().contains("iphone")) {
            return "iOS";
        } else {
            return "Unknown";
        }
    }

    /**
     * 获取用户访问浏览器
     *
     * @param request 请求
     * @return 访问浏览器
     */
    public static String getBrowser(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.toLowerCase().contains("edg")) {
            return "Microsoft Edge";
        } else if (userAgent.toLowerCase().contains("chrome")) {
            return "Google Chrome";
        } else if (userAgent.toLowerCase().contains("firefox")) {
            return "Mozilla Firefox";
        } else if (userAgent.toLowerCase().contains("safari")) {
            return "Apple Safari";
        } else if (userAgent.toLowerCase().contains("opera")) {
            return "Opera";
        } else if (userAgent.toLowerCase().contains("msie") || userAgent.toLowerCase().contains("trident")) {
            return "Internet Explorer";
        } else {
            return "Unknown";
        }
    }

    /**
     * 获取用户访问设备
     *
     * @param request 请求
     * @return 访问设备
     */
    public static String getDevice(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent.toLowerCase().contains("mobile")) {
            return "Mobile";
        }
        return "PC";
    }

    /**
     * 获取用户访问网络
     *
     * @param request 请求
     * @return 访问网络
     */
    public static String getNetwork(HttpServletRequest request) {
        // 首先尝试从HTTP头获取网络类型
        String networkType = request.getHeader("X-Online-Type");
        if (networkType != null && !networkType.isEmpty()) {
            if (networkType.equalsIgnoreCase("wifi")) {
                return "wifi";
            } else {
                return "Mobile";
            }
        }

        // 如果HTTP头没有,则尝试从用户代理字符串中推断
        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null && !userAgent.isEmpty()) {
            if (userAgent.toLowerCase().contains("mobile")) {
                return "Mobile";
            } else {
                return "wifi";
            }
        }

        // 如果以上方法都无法获取,则默认返回"unknown"
        return "unknown";
    }
}
